import re
from typing import List


def count_words(text: str) -> int:
    # For Chinese text, count characters; for other languages, count words
    chinese_chars = re.findall(r'[\u4e00-\u9fff]', text)
    english_words = re.findall(r'\b[a-zA-Z]+\b', text)
    return len(chinese_chars) + len(english_words)


def split_long_paragraph_by_sentences(paragraph: str, max_words: int = 200) -> List[str]:
    if count_words(paragraph) <= max_words:
        return [paragraph]

    sentences = re.split(r"(?<=[.!?。！？；：])\s*", paragraph)
    sentences = [s.strip() for s in sentences if s.strip()]

    if not sentences:
        return [paragraph]

    result_paragraphs = []
    current_paragraph = ""

    for sentence in sentences:
        sentence_words = count_words(sentence)
        current_words = count_words(current_paragraph)

        if not current_paragraph:
            current_paragraph = sentence
        elif current_words + sentence_words <= max_words:
            current_paragraph += " " + sentence
        else:
            if current_paragraph:
                result_paragraphs.append(current_paragraph)
            current_paragraph = sentence

    if current_paragraph:
        result_paragraphs.append(current_paragraph)

    return result_paragraphs


def split_chapter_into_paragraphs(
    chapter_content: str, min_words: int = 100, max_words: int = 50
) -> List[str]:
    sentences = re.split(r"\n\n", chapter_content)
    paragraphs = []
    current_paragraph = ""

    for sentence in sentences:
        sentence = sentence.strip()
        if not sentence:
            continue

        current_words = count_words(current_paragraph)
        sentence_words = count_words(sentence)

        if not current_paragraph:
            current_paragraph = sentence
        elif current_words + sentence_words <= max_words:
            current_paragraph += " " + sentence
        else:
            if current_words >= min_words:
                paragraphs.append(current_paragraph)
                current_paragraph = sentence
            else:
                current_paragraph += " " + sentence

    if current_paragraph:
        current_words = count_words(current_paragraph)

        if current_words < min_words and paragraphs:
            last_paragraph = paragraphs.pop()
            combined_paragraph = last_paragraph + " " + current_paragraph
            paragraphs.append(combined_paragraph)
        else:
            paragraphs.append(current_paragraph)

    final_paragraphs = []
    for paragraph in paragraphs:
        words = count_words(paragraph)
        if words < min_words and final_paragraphs:
            last_paragraph = final_paragraphs.pop()
            combined = last_paragraph + " " + paragraph
            final_paragraphs.append(combined)
        else:
            final_paragraphs.append(paragraph)

    result_paragraphs = []
    for paragraph in final_paragraphs:
        words = count_words(paragraph)
        if words > max_words:
            split_paragraphs = split_long_paragraph_by_sentences(paragraph, max_words)
            result_paragraphs.extend(split_paragraphs)
            print(f"拆分超长段落: {words}个单词 -> {len(split_paragraphs)}个段落")
        else:
            result_paragraphs.append(paragraph)

    return result_paragraphs